package com.hj.system.base.exception;

import com.hj.system.base.utils.HttpServletUtils;
import com.hj.system.base.config.Constant;
import com.hj.system.base.utils.ResponseUtil;
import com.hj.system.base.utils.ShiroUtils;
import com.hj.system.common.domain.SystemLogEntity;
import com.hj.system.common.service.LogService;
import com.hj.system.system.domain.UserDO;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Date;

/**
 * 异常处理器
 */
@RestControllerAdvice
public class BDExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private LogService logService;

    @ExceptionHandler(AuthorizationException.class)
    public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) {
        logger.error(e.getMessage(), e);
        if (HttpServletUtils.jsAjax(request)) {
            return ResponseUtil.error(403, "未授权");
        }
        return new ModelAndView("error/403");
    }

    @ExceptionHandler(CustomerRuntimeException.class)
    public Object handleCustomerRuntimeException(CustomerRuntimeException e, HttpServletRequest request, Model model) {
        logger.error(e.getMessage(), e);
        if (HttpServletUtils.jsAjax(request)) {
            return ResponseUtil.error(500, e.getMsg());
        }
        model.addAttribute("msg", e.getMsg());
        return new ModelAndView("error/500");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Object handleSqlException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return ResponseUtil.error(500, e.getCause().getMessage());
    }


    @ExceptionHandler({Exception.class})
    public Object handleException(Exception e, HttpServletRequest request) {
        SystemLogEntity systemLogEntity = new SystemLogEntity();
        systemLogEntity.setGmtCreate(new Date());
        systemLogEntity.setOperation(Constant.LOG_ERROR);
        systemLogEntity.setMethod(request.getRequestURL().toString());
        systemLogEntity.setParams(e.toString());
        UserDO current = ShiroUtils.getUser();
        if (null != current) {
            systemLogEntity.setUserId(current.getUserId());
            systemLogEntity.setUsername(current.getUsername());
        }
        logService.save(systemLogEntity);
        logger.error(e.getMessage(), e);
        if (HttpServletUtils.jsAjax(request)) {
            return ResponseUtil.error(500, "服务器错误，请联系管理员");
        }
        // response.sendRedirect("/modeler.html");
        return new ModelAndView("error/500");
    }
}
